﻿#region ... Copyright Notice ...
/*
   Copyright 2008 Tyler Jensen

	Author: Tyler Jensen    http://www.tsjensen.com/blog

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
#endregion

using System;
using System.Collections;
using System.Text;
using System.Configuration;
using System.Xml;

namespace Atrax.Configuration
{
	public class QueryProcessorConfigurationSection : ConfigurationSection
	{
		[ConfigurationProperty("queryProcessor")]
		public QueryProcessorConfigCollection QueryProcessors
		{
			get
			{
				return ((QueryProcessorConfigCollection)(base["queryProcessor"]));
			}
		}
	}

	[ConfigurationCollectionAttribute(typeof(QueryProcessorConfigElement))]
	public class QueryProcessorConfigCollection : ConfigurationElementCollection
	{
		protected override ConfigurationElement CreateNewElement()
		{
			return new QueryProcessorConfigElement();
		}

		protected override object GetElementKey(ConfigurationElement element)
		{
			return ((QueryProcessorConfigElement)(element)).Name;
		}

		public void Add(QueryProcessorConfigElement element)
		{
			this.BaseAdd(element);
		}

		public void Remove(string key)
		{
			this.BaseRemove(key);
		}

		public void Clear()
		{
			this.BaseClear();
		}

		public QueryProcessorConfigElement this[int idx]
		{
			get { return (QueryProcessorConfigElement)BaseGet(idx); }
		}

		public QueryProcessorConfigElement this[string name]
		{
			get
			{
				QueryProcessorConfigElement item = null;
				for(int i = 0; i < this.Count; i++)
				{
					if (this[i].Name.ToLower() == name.ToLower())
					{
						item = this[i];
						break;
					}
				}
				return item;
			}
		}
	}

	public class QueryProcessorConfigElement : ConfigurationElement
	{
		public QueryProcessorConfigElement() { }
		public QueryProcessorConfigElement(string name, string type)
		{
			this.Name = name;
			this.TypeName = type;
		}

		[ConfigurationProperty("name", DefaultValue = "_", IsKey = true, IsRequired = true)]
		[StringValidator(InvalidCharacters = "~!@#$%^&()[]{}/;'\"|\\", MinLength = 1, MaxLength = 260)]
		public string Name
		{
			get
			{ return (string)this["name"]; }
			set
			{ this["name"] = value; }
		}

		[ConfigurationProperty("type", DefaultValue = "", IsKey = false, IsRequired = true)]
		public string TypeName
		{
			get
			{ return this["type"].ToString(); }
			set
			{ this["type"] = value.ToString(); }
		}
	}
}

